﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


using System.Threading;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using CCWin;
namespace weiqijiance
{
    public partial class shebeixinxi : CCSkinMain
    {
        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;


        public shebeixinxi()
        {
            InitializeComponent();
            readshezhi();
            readxtxx();
            readlwxx();
            readjdxx();
            readjcxx();
        }

        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        //ini文件

        public void readshezhi()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测设备信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);



            skinComboBox1.Text = ReadValue("检测设备信息", "汽油分析仪生产企业");
            skinComboBox2.Text = ReadValue("检测设备信息", "柴油分析仪生产企业");
            skinComboBox3.Text = ReadValue("检测设备信息", "底盘测功机生产企业");
            skinComboBox4.Text = ReadValue("检测设备信息", "OBD诊断仪生产企业");

            skinComboBox5.Text = ReadValue("检测设备信息", "汽油分析仪名称");
            skinComboBox6.Text = ReadValue("检测设备信息", "柴油分析仪名称");
            skinComboBox7.Text = ReadValue("检测设备信息", "底盘测功机型号");
            skinComboBox8.Text = ReadValue("检测设备信息", "OBD诊断仪型号");

            textBox1.Text = ReadValue("检测设备信息", "汽油分析仪检定日期");
            textBox2.Text = ReadValue("检测设备信息", "柴油分析仪检定日期");
            text_tjgl.Text = ReadValue("检测设备信息", "台架惯量");

            qyjdzs.Text = ReadValue("检测设备信息", "汽油分析仪检定证书");
            cyjdzs.Text = ReadValue("检测设备信息", "柴油分析仪检定证书");

            txtqysbbh.Text = ReadValue("检测设备信息", "汽油分析仪设备编号");
            txtcysbbh.Text = ReadValue("检测设备信息", "柴油分析仪设备编号");
            txtdpcgjsbbh.Text = ReadValue("检测设备信息", "底盘测功机设备编号");
            txtobdsbbh.Text = ReadValue("检测设备信息", "OBD设备编号");
        }

        public void readxtxx()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\系统信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            text_xtxx.Text = ReadValue("系统信息", "系统名称");
            


        }
        public void readlwxx()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\联网信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            skinCom_lwcj.Text = ReadValue("设置", "联网厂家");
            text_lwdz.Text = ReadValue("设置", "联网地址");
            text_jczbh.Text = ReadValue("设置", "检测站编号");
            skinCom_jcxbh.Text = ReadValue("设置", "检测线编号");
            text_sqm.Text = ReadValue("设置", "城市行政区划编码");
            txtyhm.Text = ReadValue("设置", "用户名");
            txtpwd.Text = ReadValue("设置", "密码");
            text_lwdz2.Text = ReadValue("设置", "联网地址2");

            tb_shenhey.Text = ReadValue("设置", "审核员");
            tb_zhiliangy.Text = ReadValue("设置", "质量控制员");
            tb_shouquany.Text = ReadValue("设置", "授权人");
            tb_wenjianxnlj.Text = ReadValue("设置", "文件虚拟路径");
            tb_wenjianlj.Text = ReadValue("设置", "文件路径");
            tb_tongdaoq.Text = ReadValue("设置", "工位前通道");
            tb_tongdaoz.Text = ReadValue("设置", "工位中通道");
            tb_tongdaoh.Text = ReadValue("设置", "工位后通道");

        }

        public void readjdxx()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检定系数设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);



            text_glscxs.Text = ReadValue("设置", "惯量输出系数");
            


        }
        public void readjcxx()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测参数设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            text_lug100.Text = ReadValue("设置", "全负荷扫描100点控制系数");
            text_lug80.Text = ReadValue("设置", "全负荷扫描80点控制系数");
            text_menkan.Text = ReadValue("设置", "输出模块门槛值");
            text_TL_FQY.Text = ReadValue("设置", "ASM调零时间/秒");
            text_TL_BTG.Text = ReadValue("设置", "LUG调零时间/秒");
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            baocunlwxx();//联网信息
            baocunshezhi();
            baocunxtxx();//系统信息
            baocunjcxx();//检测参数设置
        }
        public void baocunshezhi()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测设备信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            Writue("检测设备信息", "汽油分析仪生产企业",skinComboBox1.Text);
            Writue("检测设备信息", "柴油分析仪生产企业", skinComboBox2.Text);
            Writue("检测设备信息", "底盘测功机生产企业", skinComboBox3.Text);
            Writue("检测设备信息", "OBD诊断仪生产企业", skinComboBox4.Text);

            Writue("检测设备信息", "汽油分析仪名称", skinComboBox5.Text);
            Writue("检测设备信息", "柴油分析仪名称", skinComboBox6.Text);
            Writue("检测设备信息", "底盘测功机型号", skinComboBox7.Text); ;
            Writue("检测设备信息", "OBD诊断仪型号", skinComboBox8.Text);

            Writue("检测设备信息", "汽油分析仪检定日期", textBox1.Text);
            Writue("检测设备信息", "柴油分析仪检定日期", textBox2.Text);
            Writue("检测设备信息", "台架惯量", text_tjgl.Text);

            Writue("检测设备信息", "汽油分析仪检定证书", qyjdzs.Text);
            Writue("检测设备信息", "柴油分析仪检定证书", cyjdzs.Text);

            Writue("检测设备信息", "汽油分析仪设备编号", txtqysbbh.Text);
            Writue("检测设备信息", "柴油分析仪设备编号", txtcysbbh.Text);
            Writue("检测设备信息", "底盘测功机设备编号", txtdpcgjsbbh.Text);
            Writue("检测设备信息", "OBD设备编号", txtobdsbbh.Text);

            

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "检测设备信息设置.ini");

            string sql = string.Format(@"select * from WQ_SHEBEI where WQ_XIANHAO='{0}'", skinCom_jcxbh.Text);
            DataTable dt = new DataTable();
            dt = m_mainform.objbass.objdb.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                sql = string.Format(@"update  WQ_SHEBEI set WQ_QYFXYQY='{0}',WQ_QYFXYMC='{1}',WQ_QYFXYRQ='{2}',WQ_QYFXYZS='{3}',
                WQ_CYFXYQY='{4}',WQ_CYFXYMC='{5}',WQ_CYFXYRQ='{6}',WQ_CYFXYZS='{7}',WQ_DPQY='{8}',WQ_DPXH='{9}',WQ_OBDQY='{10}',WQ_OBDXH='{11}' 
                where WQ_XIANHAO='{12}'",
                                        skinComboBox1.Text,
                                        skinComboBox5.Text,
                                        textBox1.Text,
                                        qyjdzs.Text,
                                        skinComboBox2.Text,
                                        skinComboBox6.Text,
                                        textBox2.Text,
                                        cyjdzs.Text,
                                        skinComboBox3.Text,
                                        skinComboBox7.Text,
                                        skinComboBox4.Text,
                                        skinComboBox8.Text,
                                        skinCom_jcxbh.Text);
            }
            else
            {
                sql = string.Format(@"insert WQ_SHEBEI (WQ_QYFXYQY,WQ_QYFXYMC,WQ_QYFXYRQ,WQ_QYFXYZS,WQ_CYFXYQY,WQ_CYFXYMC,WQ_CYFXYRQ,
                                    WQ_CYFXYZS,WQ_DPQY,WQ_DPXH,WQ_OBDQY,WQ_OBDXH,WQ_XIANHAO) values 
                                    ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')", skinComboBox1.Text,
                                        skinComboBox5.Text,
                                        textBox1.Text,
                                        qyjdzs.Text,
                                        skinComboBox2.Text,
                                        skinComboBox6.Text,
                                        textBox2.Text,
                                        cyjdzs.Text,
                                        skinComboBox3.Text,
                                        skinComboBox7.Text,
                                        skinComboBox4.Text,
                                        skinComboBox8.Text,
                                        skinCom_jcxbh.Text);
            }
            m_mainform.objbass.objdb.InsertDB(sql);
        
        }


        public void baocunxtxx()//系统信息
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\系统信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            Writue("系统信息", "系统名称", text_xtxx.Text);
           

        }

        public void baocunjcyqsz()//系统信息
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测要求设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            Writue("设置", "汽油车转速取值", text_xtxx.Text);

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "检测要求设置.ini");

        }

        public void baocunlwxx()//联网信息
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\联网信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            Writue("设置", "联网厂家", skinCom_lwcj.Text);
            Writue("设置", "联网地址", text_lwdz.Text);
            Writue("设置", "城市行政区划编码", text_sqm.Text);
            Writue("设置", "检测站编号", text_jczbh.Text);
            Writue("设置", "检测线编号", skinCom_jcxbh.Text);

            Writue("设置", "联网地址2", text_lwdz2.Text);
            Writue("设置", "用户名", txtyhm.Text);
            Writue("设置", "密码", txtpwd.Text);


            Writue("设置", "审核员", tb_shenhey.Text);
            Writue("设置", "质量控制员", tb_zhiliangy.Text);
            Writue("设置", "授权人", tb_shouquany.Text);
            Writue("设置", "文件虚拟路径", tb_wenjianxnlj.Text);
            Writue("设置", "文件路径", tb_wenjianlj.Text);
            Writue("设置", "工位前通道", tb_tongdaoq.Text);
            Writue("设置", "工位中通道", tb_tongdaoz.Text);
            Writue("设置", "工位后通道", tb_tongdaoh.Text);

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "联网信息设置.ini");
        }

        public void baocunjdxx()//检定系数设置
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检定系数设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            Writue("设置", "惯量输出系数", text_glscxs.Text);

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "检定系数设置.ini");

        }
        public void baocunjcxx()//检测参数设置
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测参数设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            Writue("设置", "全负荷扫描100点控制系数", text_lug100.Text);
            Writue("设置", "全负荷扫描80点控制系数", text_lug80.Text);
            Writue("设置", "输出模块门槛值", text_menkan.Text);
            Writue("设置", "ASM调零时间/秒", text_TL_FQY.Text);
            Writue("设置", "LUG调零时间/秒", text_TL_BTG.Text);

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "检测参数设置.ini");
     
 


        }

        public mainform m_mainform;// { get; set; }
        public virtual void setmainform(mainform m_m)
        {
            m_mainform = m_m;
        }
        private void skinButton2_Click(object sender, EventArgs e)
        {
            DateTime date = new DateTime();
            if (!DateTime.TryParse(textBox1.Text, out date))
            {
                MessageBox.Show("日期输入格式错误，请改正后再传");
                return;
            }
            m_mainform.objbass.uploadjd(date.ToString("yyyy-MM-dd"), date.AddYears(1).ToString("yyyy-MM-dd"), qyjdzs.Text);
        }
    }
}
